package org.jdesktop.swingx.input;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoPosition;

import vnreal.ZNEW.GraphTest.Node;
import vnreal.ZNEW.GraphTest.SubstrateLink;
import vnreal.ZNEW.GraphTest.WorkOnPaths;
import vnreal.gui.menu.MapMenu;
import vnreal.map.FancyWaypointRenderer;
import vnreal.map.Map;
import vnreal.map.MyWaypoint;

/**
 * Centers the map on the mouse cursor if Right is double-clicked or middle
 * mouse button is pressed.
 * 
 * @author Martin Steiger
 * @author joshy
 */
public class EditNode {
	static JDialog dialog;
	private JXMapViewer viewer;
	static String selected;

	public EditNode(JXMapViewer viewer) {
		this.viewer = viewer;
		findNode();
		openDialog(viewer);
		fillFields();
	}
	private ActionListener addClicked = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			new NewLink2(viewer);
			dialog.setEnabled(false);
		}
	};
	private ActionListener editClicked = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			if(links.getItemCount() > 0) {
				selected = (String) links.getSelectedItem();
				new EditLink2(viewer);
				dialog.setEnabled(false);
			}
		}
	};
	private ActionListener removeClicked = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			if(links.getItemCount() > 0) {
				selected = (String) links.getSelectedItem();
				int i = -1;
				for (SubstrateLink link : WorkOnPaths.substrateEdges) {
					++i;
					if (link.src != null) {
						if (link.src.globalNodeId.equals(selected)) {
							if(link.dst.globalNodeId.equals(node.globalNodeId)) {
								break;
							}
						}
					}
				}
				WorkOnPaths.substrateEdges.remove(i);
				i = -1;
				for (SubstrateLink link : WorkOnPaths.substrateEdges) {
					++i;
					if (link.dst != null) {
						if (link.dst.globalNodeId.equals(selected)) {
							if(link.src.globalNodeId.equals(node.globalNodeId)) {
								break;
							}
						}
					}
				}
				WorkOnPaths.substrateEdges.remove(i);
				i = links.getSelectedIndex();
				comboBoxItems.remove(i);
				if(i > 0) {
					links.setSelectedIndex(--i);
				}
				else if (i == 0) {
					if(!comboBoxItems.isEmpty()) {
						links.setSelectedIndex(i);
					}
				}
			}
		}
	};
	private ActionListener cancelClicked = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			dialog.dispose();
			Nodes.dialog.setEnabled(true);
		}
	};
	private ActionListener okClicked = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			boolean isNumber = true;
			Nodes.dialog.setEnabled(true);
			
			try {
				Double.valueOf(energy.getText());
			} catch (NumberFormatException ex) {
				isNumber = false;
			} catch (NullPointerException ex) {
				isNumber = false;
			}
			if (isNumber) {
				node.name = name.getText();
				node.energy = energy.getText();
				dialog.dispose();
			} else {
				JOptionPane.showMessageDialog(dialog,
						"Error!", "Error!",
						JOptionPane.WARNING_MESSAGE);
			}
		}
	};
	Node node = new Node();
	private JTextField name;
	private JTextField energy;
	public static JComboBox links;
	static Vector<String> comboBoxItems;
	
	private void fillFields() {
		name.setText(node.name);
		energy.setText(node.energy);
	}
	private void findNode() {
		for(Node node : WorkOnPaths.substrateNodes) {
			if(node.globalNodeId.equals(Nodes.selected)) {
				this.node = node;
			}
		}
	}
	private void openDialog(JXMapViewer viewer) {
		dialog = new JDialog(MapMenu.frame, "Edit Node");
		JLabel label;
		dialog.setLayout(new GridLayout(10,2));
		dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		label = new JLabel("   ID");
		dialog.add(label);

		label = new JLabel(node.globalNodeId);
		dialog.add(label);

		label = new JLabel("   Name");
		dialog.add(label);

		name = new JTextField();
		dialog.add(name);

		label = new JLabel("   Latitide");
		dialog.add(label);

		label = new JLabel(String.valueOf(node.latitude));
		dialog.add(label);
		
		label = new JLabel("   Longitude");
		dialog.add(label);

		label = new JLabel(String.valueOf(node.longitude));
		dialog.add(label);

		label = new JLabel("   Energy");
		dialog.add(label);

		energy = new JTextField();
		dialog.add(energy);
		
		JButton addbutton = new JButton("Add");
		addbutton.addActionListener(addClicked);
		dialog.add(addbutton);

		comboBoxItems = new Vector<String>();
		for (SubstrateLink link : WorkOnPaths.substrateEdges) {
			if(link.src.globalNodeId.equals(node.globalNodeId)) {
				comboBoxItems.add(link.dst.globalNodeId);
			}
		}
		final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(
				comboBoxItems);
		links = new JComboBox(model);
		dialog.add(links);

		JButton editbutton = new JButton("Edit");
		editbutton.addActionListener(editClicked);
		dialog.add(editbutton);
		
		dialog.add(new JLabel());

		JButton removebutton = new JButton("Remove");
		removebutton.addActionListener(removeClicked);
		dialog.add(removebutton);
		
		dialog.add(new JLabel());
		dialog.add(new JLabel());
		dialog.add(new JLabel());

		JButton okButton = new JButton("OK");
		okButton.addActionListener(okClicked);
		dialog.add(okButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(cancelClicked);
		dialog.add(cancelButton);

		dialog.setSize(500, 200);
		dialog.setResizable(false);
		// dialog.setAlwaysOnTop(true);
		dialog.setVisible(true);
	}
}
